package com.example.homework.ui;

import android.os.Bundle;
import android.view.ActionMode;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.homework.ContactArrayAdapter;
import com.example.homework.DBHelper;
import com.example.homework.MainActivity;
import com.example.homework.R;
import com.example.homework.model.Contact;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class BlockedFragment extends Fragment {
    private ActionMode actionMode;
    private ListView listView;
    private Contact selectedContact;
    private ContactArrayAdapter contactArrayAdapter;
    private DBHelper dbHelper;
    private ActionMode.Callback callback=new ActionMode.Callback() {
        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            MenuInflater inflater=mode.getMenuInflater();
            inflater.inflate(R.menu.blocked_action_menu,menu);
            return true;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            if (item.getItemId()==R.id.unblock_menu){
                dbHelper.updateBlock(dbHelper.getWritableDatabase(),selectedContact.getId(),false);
                contactArrayAdapter.remove(selectedContact);
                contactArrayAdapter.notifyDataSetChanged();
            }
            actionMode.finish();
            return true;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {
            actionMode=null;
            selectedContact=null;
        }
    };

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.blocked_layout,container,false);
        listView=view.findViewById(R.id.blocked_contact_list);
        this.contactArrayAdapter=new ContactArrayAdapter(getActivity().getApplicationContext(), R.layout.contact_item_layout);
        fillData(contactArrayAdapter);
        listView.setAdapter(contactArrayAdapter);
        listView.setOnItemLongClickListener((AdapterView<?> parent, View v, int position, long id)->{
            if (actionMode!=null){
                return false;
            }
            actionMode= getActivity().startActionMode(callback);
            v.setSelected(true);
            selectedContact=contactArrayAdapter.getItem(position);
            return true;
        });
        this.dbHelper=new DBHelper(getContext());
        return view;
    }
    public void fillData(ContactArrayAdapter arrayAdapter){
        MainActivity mainActivity=(MainActivity) getActivity();
        List<Contact> contacts=mainActivity.dbHelper.queryAll(mainActivity.dbHelper.getReadableDatabase())
                .stream().filter(Contact::isBlocked).collect(Collectors.toList());
        arrayAdapter.clear();
        arrayAdapter.setContacts(contacts);
    }

    @Override
    public void onResume() {
        super.onResume();
        fillData(contactArrayAdapter);
    }
}
